package com.gdyx.app.web.annotation;

import com.gdyx.app.web.model.MpsFun;
import com.gdyx.app.web.model.MpsFunOpt;
import com.gdyx.app.web.model.MpsUser;
import com.gdyx.app.web.svc.MpsOrgSvc;
import com.gdyx.app.web.svc.MpsOrgUserSvc;
import com.gdyx.app.web.util.Condition;
import com.gdyx.app.web.web.BaseController;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 权限注释解析器
 * 有注解则解析出注解中的name、uri(权限名和权限URI)
 * Created by Y_Coffee on 2017/7/24.
 */
@Aspect
@Component
public class AuthorityAnnotationParse {
    @Autowired
    HttpServletRequest request;
    @Autowired
    MpsOrgUserSvc mpsOrgUserSvc;
    @Autowired
    MpsOrgSvc mpsOrgSvc;


    @Pointcut("@annotation(authorisation)")
    public void annotationPointCut(Authorisation authorisation) {

    }

    @Around("annotationPointCut(authorisation)")
    public Object around(ProceedingJoinPoint thisJoinPoint, Authorisation authorisation) throws Throwable {
//        MapperMethod.MethodSignature sign = (MapperMethod.MethodSignature) joinPoint.getSignature();
        String permission = authorisation.value();
        ModelAndView view = new ModelAndView();
        view.addObject("respMsg", "抱歉，您无权访问该功能！");
        view.setViewName("index");
        //是否登录
        MpsUser mpsUser = (MpsUser) request.getSession().getAttribute("ADMIN_SESSION");
        if (null == mpsUser) {
            view.addObject("respMsg", "请先登录");
            view.setViewName("login");
            return view;
        } else {
            //权限控制业务
            //拥有的权限
            List<MpsFun> mpsFunList = (List<MpsFun>) request.getSession().getAttribute("WEBFUNS");
            List<MpsFunOpt> mpsFunOptList = (List<MpsFunOpt>) request.getSession().getAttribute("WEBOPTS");
            //单个或者多个，分割符啥的可以自己定义
            for (MpsFun mpsFun : mpsFunList) {
                if (mpsFun.getHref().equals(permission)) {
                    BaseController baseController = new BaseController();
                    request.getSession().setAttribute("ADMIN_MENUS", baseController.doMenus(mpsFunList, permission));
                    return thisJoinPoint.proceed();
                }
            }
            for (MpsFunOpt mpsFunOpt : mpsFunOptList) {
                if (mpsFunOpt.getHref().equals(permission)) {
                    return thisJoinPoint.proceed();
                }
            }


        }
        return view;
    }
    public Condition ruleBuild(String connnctor, String column, String operator, java.lang.Object value, String type) {
        Condition condition = new Condition();
        try {
            condition.setConnector(connnctor);
            condition.setColumn(column);
            condition.setOperator(operator);
            condition.setType(type);
            condition.setValue(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return condition;
    }

}
